﻿package com.saint.utils.ui{
	
	import flash.display.Sprite;
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import com.saint.utils.events.ActiveMouseAreaEvent;
	import com.saint.utils.Logger;
	
	public class ActiveMouseArea extends Sprite {
		
		private var _Colour:uint = 0xFF0099;
		private var _Alpha:Number = 0;
		private var _Width:uint;
		private var _Height:uint;
		private var bg:*;
		private var ro:*;
		
		public function ActiveMouseArea(w:uint = 50, h:uint = 50, _bg:* = null, _ro:* = null){
			
			_Width = w;
			_Height = h;
			
			if(_bg != null){
				bg = new _bg;
				addChild(bg);
			}else {
				drawBg();
			}
			
			if(_ro != null){
				ro = new _ro;
				ro.visible = false;
				addChild(ro);
			}
			
			setEnabled(true);
		}
		
		private function handleMouseUp(e:MouseEvent):void{
			if(ro != null){
				ro.visible = false;
			}
		}
		
		private function handleMouseDown(e:MouseEvent):void {
			handleMouseMove(null);
			stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp, false, 0, true);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove, false, 0, true);
		}
		
		private function handleMouseMove(e:MouseEvent):void {
			var percentage:Number = (_Width / 100) * mouseX;
			if(percentage >= 0 && percentage <= 1){
				Logger.info("ActiveMouseArea moving " + (percentage * 100) + "%");
				dispatchEvent(new ActiveMouseAreaEvent(ActiveMouseAreaEvent.MOUSE_MOVE, percentage));
			}
		}
		
		private function handleMouseOver(e:MouseEvent):void {
			if(ro != null){
				ro.visible = true;
			}
		}
		
		private function handleMouseOut(e:MouseEvent):void{
			if (ro != null) {
				ro.visible = false;
			}
		}
		
		protected function onStageMouseUp(event:MouseEvent):void {
			Logger.info("ActiveMouseArea.onStageMouseUp ");
			if (event.target == this ){
				// already handled
			}else{
				handleMouseUp(null);
			}
			
			try {
				stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
			}catch(err:Error) {
				Logger.debug("ActiveMouseArea - no stage events to remove");
			}
		}
		
		public function setEnabled(TF:Boolean):void{
			if(TF){
				buttonMode = true;
				mouseChildren = false;
				addEventListener(MouseEvent.ROLL_OVER, handleMouseOver, false, 0, true);
				addEventListener(MouseEvent.ROLL_OUT, handleMouseOut, false, 0, true);
				addEventListener(MouseEvent.MOUSE_UP, handleMouseUp, false, 0, true);
				addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown, false, 0, true);
			}else{
				buttonMode = false;
				removeEventListener(MouseEvent.ROLL_OVER, handleMouseOver);
				removeEventListener(MouseEvent.ROLL_OUT, handleMouseOut);
				removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
				removeEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			}
		}
		
		private function drawBg():void {
			graphics.beginFill(_Colour, _Alpha);
			graphics.drawRect(0, 0, _Width, _Height);
			graphics.endFill();
		}
		
		public function destroy():void {
			Logger.info("GraphicBttn.destroy");
			if (hasEventListener(MouseEvent.ROLL_OVER)){
				removeEventListener(MouseEvent.ROLL_OVER, handleMouseOver);
				removeEventListener(MouseEvent.ROLL_OUT, handleMouseOut);
				removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
				removeEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			}
		}
		
		
	}
}